package com.ofd.sign.exception.handler;

import com.ofd.sign.domin.SignRes;
import com.ofd.sign.exception.AuthException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

import static java.util.stream.Collectors.toList;


@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public SignRes<Void> generateExceptionHandler(Exception e) {
        log.error("服务器错误:",e);
        return SignRes.fail("服务器错误");
    }


    /**
     * 鉴权异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(AuthException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public SignRes<Void> handleException(AuthException e) {
        return SignRes.fail(e.getMessage());
    }


    /**
     * 参数不匹配异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public SignRes<Void> handleException(MissingServletRequestParameterException e) {
        return SignRes.fail("参数错误");
    }

    /**
     * 处理校验异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    public SignRes<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        return SignRes.fail("参数错误");
    }

}
